package com.design.android.personnel.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import com.design.android.personnel.AppContext;
import com.design.android.personnel.MainActivity;
import com.design.android.personnel.R;
import com.design.android.personnel.bean.LoggedInUser;
import com.design.android.personnel.bean.User;
import com.design.android.personnel.bean.UserLoginLog;
import com.design.android.personnel.database.UserLoginLogOperate;
import com.design.android.personnel.database.UserOperate;
import com.design.android.personnel.utils.Md5Utils;

import java.util.Date;

/**
 * @author admin
 */
public class LoginActivity extends AppCompatActivity {

    private EditText etPhone, etPassWord;
    private Button btReg, btLogin;
    private CheckBox cbPWDdVisibility;

    private UserOperate userOperate;
    private UserLoginLogOperate userLoginLogOperate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setTitle(getString(R.string.title_activity_login));

        etPhone = findViewById(R.id.login_name);
        etPassWord = findViewById(R.id.login_passWord);

        btReg = findViewById(R.id.login_reg);
        btLogin = findViewById(R.id.login_login);

        cbPWDdVisibility = findViewById(R.id.login_checkBox);

        userOperate = new UserOperate(this);
        userLoginLogOperate = new UserLoginLogOperate(this);

        // 初始化EditText
        initEditText();

        cbPWDdVisibility.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    // 如果选中，显示密码
                    etPassWord.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    // 否则隐藏密码
                    etPassWord.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
        // 注册
        btReg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jump2Register();
            }
        });

        // 登录
        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userLogin();
            }
        });
    }


    private void initEditText() {
        Intent intent = getIntent();
        String uName = intent.getStringExtra("phone");
        String uPassWord = intent.getStringExtra("passWord");
        if (uName == null && uPassWord == null) {
            etPhone.setText("13344445555");
            etPassWord.setText("123456");
            // 把光标设置到当前文本末尾
            etPhone.setSelection(etPhone.getText().length());
        } else {
            etPhone.setText(uName);
            etPassWord.setText(uPassWord);
        }
    }

    private void userLogin() {
        String phone = etPhone.getText().toString();
        String passWord = etPassWord.getText().toString();
        if (phone.isEmpty()) {
            etPhone.requestFocus();
            etPhone.setError(getString(R.string.a_login_userNameEmptyError));
            return;
        }
        if (passWord.isEmpty()) {
            etPassWord.requestFocus();
            etPassWord.setError(getString(R.string.a_login_userPwdEmptyError));
            return;
        }

        User select = userOperate.select(phone);

        UserLoginLog userLoginLog = new UserLoginLog();
        userLoginLog.setLoginAccount(phone);
        userLoginLog.setLoginTime(new Date());
        userLoginLog.setResult(String.valueOf(false));

        if (select != null) {
            // 账户存在

            String salt = select.getSalt();

            String hash = Md5Utils.hash(passWord.concat(salt));

            boolean equals = select.getPassword().equals(hash);

            if (equals) {
                // 登录成功
                LoggedInUser loggedInUser = new LoggedInUser();
                loggedInUser.setLoginTime(new Date());
                loggedInUser.setUser(select);

                AppContext.setLoggedInUser(loggedInUser);

                userLoginLog.setResult(String.valueOf(true));
                userLoginLogOperate.add(userLoginLog);

                jump2Main();
            } else {
                // 密码错误
                etPassWord.requestFocus();
                String text = getString(R.string.a_login_userPwdError);
                etPassWord.setError(text);
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
                userLoginLog.setResultInfo(text);
            }
        } else {
            etPhone.requestFocus();
            String text = getString(R.string.a_login_noUserNameError);
            etPhone.setError(text);
            userLoginLog.setResultInfo(text);
            Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
        }

        userLoginLogOperate.add(userLoginLog);
    }

    private void jump2Register() {
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(), RegisterActivity.class);
        startActivity(intent);
    }

    private void jump2Main() {
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(), MainActivity.class);
        startActivity(intent);
        finish();
    }
}
